package com.sync.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel.MapMode;

public class BockReadVsMapBufferInRAMFSY {
	public static void main(String[] args) throws IOException {
		RandomAccessFile randomAccessFile = null;
		MappedByteBuffer mappedByteBuffer = null;
		// ByteBuffer tmp = ByteBuffer.allocate(4 * 1024 * 1024);
		byte[] tmp = new byte[4 * 1024 * 1024];
		long start = 0;
		long len = 0;
		int size = 0;;
		try {
			randomAccessFile = new RandomAccessFile("Z:/1.txt", "rw");
			mappedByteBuffer = randomAccessFile.getChannel()
					.map(MapMode.READ_WRITE, 0, randomAccessFile.length());
			len = randomAccessFile.length();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		start = System.currentTimeMillis();
		for (int i = 0; i < len;) {
			randomAccessFile.seek(i);// 移动文件指针位置
			randomAccessFile.read(tmp);
			i += 4 * 1024 * 1024;
		}
		System.err.println(System.currentTimeMillis() - start);
		start = System.currentTimeMillis();
		for (int i = 0; i < len; ++i) {
			size = mappedByteBuffer.remaining();
			size = 4 * 1024 * 1024 < size ? 4 * 1024 * 1024 : size;
			mappedByteBuffer.get(tmp, 0, size);
		}
		System.err.println(System.currentTimeMillis() - start);
	}
}
